from typing import List
#
#
# class Solution:
#     def twoSum(self, nums: List[int], target: int) -> List[int]:
#         n = len(nums)
#         for i in range(n):
#             for j in range(i + 1, n):
#                 if nums[i] + nums[j] == target:
#                     print([i, j])
#                     return [i, j]
#
# if __name__ == '__main__':
#     nums = [2,7,11,15]
#     target = 9
#     s = Solution()
#     s.twoSum(nums, target)


#哈希表写法
from typing import List


class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hashtable = dict()      #创建一个字典
        print(hashtable)
        for i, num in enumerate(nums):  #enumerate将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列   i是下标，num是值
            if target - num in hashtable.keys():
                print(hashtable[target - num], i)
                return [hashtable[target - num], i]
            hashtable[nums[i]] = i      #把nums列表里面的值和索引存入哈希表
            print(hashtable)
        # return []
if __name__ == '__main__':
    nums = [11,7,2,15]
    target = 9
    s = Solution()
    s.twoSum(nums, target)


